package com.starlight.flower_server.domain.service.impl;

import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.item.data.ItemData;
import com.starlight.flower_server.domain.itemCategory.data.ItemCategoryData;
import com.starlight.flower_server.domain.itemCategory.data.ItemCategoryWithAllSubData;
import com.starlight.flower_server.domain.service.ItemCategoryService;
import com.starlight.flower_server.infrastructure.entity.Item;
import com.starlight.flower_server.infrastructure.entity.ItemCategory;
import com.starlight.flower_server.infrastructure.repository.ItemCategoryRepository;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/5/22 19:19
 */
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Resource
    private ItemCategoryRepository itemCategoryRepository;

    @Override
    public ItemCategory queryItemCategoryDetailById(Long id) {
        return itemCategoryRepository.queryDetail(id);
    }

    @Override
    public Integer logicDeleteItemCategoryById(Long id) {
        return itemCategoryRepository.logicDelete(id);
    }

    @Override
    public Integer insertItemCategory(ItemCategory itemCategory) {
        return itemCategoryRepository.insert(itemCategory);
    }

    @Override
    public Integer modifyItemCategory(ItemCategory itemCategory) {
        return itemCategoryRepository.insert(itemCategory);
    }

    @Override
    public ItemCategoryWithAllSubData querySubItemCategory(Long parentId) {
        List<ItemCategory> itemCategoryList=itemCategoryRepository.querySub(parentId);
        ItemCategory parentItemCategory=itemCategoryRepository.queryDetail(parentId);
        ItemCategoryWithAllSubData parentItemCategoryData=new ItemCategoryWithAllSubData();
        BeanUtils.copyProperties(parentItemCategory,parentItemCategoryData);
        List<ItemCategoryData> itemCategoryDataList=itemCategoryList
                .stream().map(itemCategory -> {
                    ItemCategoryData itemCategoryData=new ItemCategoryData();
                    BeanUtils.copyProperties(itemCategory,itemCategoryData);
                    return itemCategoryData;
                }).toList();
        parentItemCategoryData.setSubCategoryData(itemCategoryDataList);
        return parentItemCategoryData;
    }

    @Override
    public TotalData<ItemCategoryData> queryAllItemCategoryData() {
        TotalData<ItemCategoryData> data=new TotalData<>();
        List<ItemCategory> itemCategoryList=itemCategoryRepository.queryAll();
        List<ItemCategoryData> itemCategoryDataList=itemCategoryList
                .stream().map(itemCategory -> {
                    ItemCategoryData itemCategoryData=new ItemCategoryData();
                    BeanUtils.copyProperties(itemCategory,itemCategoryData);
                    return itemCategoryData;
                }).toList();
        Long total=(long)itemCategoryDataList.size();
        data.setTable("Item");
        data.setStatTime(new Date());
        data.setTotal(total);
        data.setRecords(itemCategoryDataList);
        return data;
    }

}
